<script setup lang="ts">
import { ref, reactive } from "vue";
import { CategoryRules } from "@/view/Category/utils/rules.ts";
import { Plus } from "@element-plus/icons-vue";
import { addSubCategoryAPI, editSubCategoryAPI, deleteSubCategoryAPI } from "@/api/creation.ts";
import { ElMessage } from "element-plus";
import { useCategoryStore } from "@/store/category.ts";

const dialogVisible = ref(false)
const mainId = ref('')
const subId = ref('')
const categoryStore = useCategoryStore()
const typeRef = ref(0)
const actionUrl = import.meta.env.VITE_UPLOAD_SUBCATEGORY_COVER
const ruleForm = reactive({
  name: '',
  nickname: '',
  desc: '',
  cover: ''
})

// 图片上传成功
const onSuccess = (response: any) => {
  ruleForm.cover = response.data
}

// 初始
const init = (id: string, row: any, number: number) => {
  dialogVisible.value = true
  mainId.value = id
  subId.value = ''
  typeRef.value = number
  ruleForm.name = ''
  ruleForm.nickname = ''
  ruleForm.desc = ''
  ruleForm.cover = ''
  if (number) {
    // 编辑
    subId.value = row._id
    ruleForm.name = row.name
    ruleForm.nickname = row.nickname
    ruleForm.desc = row.desc
    ruleForm.cover = row.cover
  }
}

// 确认
const handleCommit = async () => {
  const res = await addSubCategoryAPI(ruleForm, mainId.value)
  ElMessage({
    type: res.code === 0 ? "success" : "error",
    message: res.message
  })
  await categoryStore.getCategoryList()
  handleClose()
}

// 编辑
const handleEdit = async () => {
  const res = await editSubCategoryAPI(subId.value, ruleForm)
  ElMessage({
    type: res.code === 0 ? "success" : "error",
    message: res.message
  })
  await categoryStore.getCategoryList()
  handleClose()
}

// 删除二级分类
const handleDelete = async () => {
  const res = await deleteSubCategoryAPI(subId.value)
  ElMessage({
    type: res.code === 0 ? "success" : "error",
    message: res.message
  })
  await categoryStore.getCategoryList()
  handleClose()
}

// 关闭弹框
const handleClose = () => {
  dialogVisible.value = false
}

defineExpose({
  init
})
</script>

<template>
  <el-dialog v-model="dialogVisible" :title="typeRef === 0 ? '添加二级分类' : '编辑二级分类'">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="CategoryRules"
      style="padding: 0 50px"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" :rows="4" resize="none" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload :action="actionUrl"
                   :on-success="onSuccess"
                   list-type="picture-card">
          <el-icon>
            <Plus/>
          </el-icon>
        </el-upload>
        <el-button @click="handleDelete" v-if="typeRef === 1" style="margin-left: 50px" type="primary">删除分类</el-button>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" v-if="typeRef === 0" @click="handleCommit">
          确定
        </el-button>
        <el-button type="primary" v-else @click="handleEdit">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
